{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "16a0c1b9-7679-48a9-bd98-67caf2123963",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Hotspots\n",
    "\n",
    "Hotspots California map: \n",
    "- Wildifre-wildifreSmokePM2.5, \n",
    "- Heat-wildfire,\n",
    "- Heat-PM2.5, \n",
    "- Wildifre-Heat-SmokePM2.5P"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ab11051-f28d-48be-8867-bd37d5046b9c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a85591f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df = pd.read_parquet(\"outputs/d1-heat-wf-smokeday-230929.parquet\")\n",
    "# df.to_parquet(\"outputs/d1-heat-wf-smokeday-230929.parquet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a7a8fdd1-262c-4e60-9d5a-71d320ee773b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df = df.drop(columns = ['tmax', 'smoke_pm']) # ['tmin','tmax','pm25','smoke_pm'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3abbfd11-43fa-4c54-b172-7474c3bf8f7d",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Find hotspots\n",
    "\n",
    "Terminology:\n",
    "- h heat\n",
    "- w wildfire\n",
    "- p polluted\n",
    "- s smoke polluted\n",
    "\n",
    "Example:\n",
    "- hwp is a hotspot where heat wildfire and pollution are co-occurring \n",
    "\n",
    "- _hws is where heat OR wildfire OR smoke are occurring on the same day\n",
    "\n",
    "- hws_2d is where heat wildfire and smoke are occurring within 24 hours window (2 days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e48a5bd0-1ce1-46b4-867d-441fb10e4308",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df['hw'] = df['wfday'] & df['heatday']\n",
    "#df['hp'] = df['heatday'] & df['polluted']\n",
    "#df['hwp'] = df['heatday'] & df['wfday'] & df['polluted']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f47e2221-bbcc-4215-9cd8-2fcce3d6ebc6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df['_hws'] = df[['heatday','wfday','smoke_pm_non_zero']].any(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d515cb23-1779-433e-b6ea-b12af418bb60",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# smoke_pm_non_zero taken as main\n",
    "\n",
    "df['hs'] = df['heatday'] & df['smoke_pm_non_zero']\n",
    "df['hws'] = df['heatday'] & df['wfday'] & df['smoke_pm_non_zero']\n",
    "df['ws'] = df['wfday'] & df['smoke_pm_non_zero']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "11a0b1ee-3900-40ad-9739-c0d63d1c6da3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df['hs5'] = df['heatday'] & df['smoke_pm_gt_five']\n",
    "df['hws5'] = df['heatday'] & df['wfday'] & df['smoke_pm_gt_five']\n",
    "df['ws5'] = df['wfday'] & df['smoke_pm_gt_five']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3f1c5a62-6aa5-4605-a5ae-022063f32888",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "#df = df.drop(columns=['index', 'FIPS_1'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "09954dfd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>GEOID</th>\n",
       "      <th>wfday</th>\n",
       "      <th>heatday</th>\n",
       "      <th>smoke_pm_non_zero</th>\n",
       "      <th>smoke_pm_gt_five</th>\n",
       "      <th>hw</th>\n",
       "      <th>_hws</th>\n",
       "      <th>hs</th>\n",
       "      <th>hws</th>\n",
       "      <th>ws</th>\n",
       "      <th>hs5</th>\n",
       "      <th>hws5</th>\n",
       "      <th>ws5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2006-01-01</td>\n",
       "      <td>04001942600</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2006-01-01</td>\n",
       "      <td>04001942700</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2006-01-01</td>\n",
       "      <td>04001944000</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2006-01-01</td>\n",
       "      <td>04001944100</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2006-01-01</td>\n",
       "      <td>04001944201</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        time        GEOID  wfday  heatday  smoke_pm_non_zero  \\\n",
       "0 2006-01-01  04001942600  False    False              False   \n",
       "1 2006-01-01  04001942700  False    False              False   \n",
       "2 2006-01-01  04001944000  False    False              False   \n",
       "3 2006-01-01  04001944100  False    False              False   \n",
       "4 2006-01-01  04001944201  False    False              False   \n",
       "\n",
       "   smoke_pm_gt_five     hw   _hws     hs    hws     ws    hs5   hws5    ws5  \n",
       "0             False  False  False  False  False  False  False  False  False  \n",
       "1             False  False  False  False  False  False  False  False  False  \n",
       "2             False  False  False  False  False  False  False  False  False  \n",
       "3             False  False  False  False  False  False  False  False  False  \n",
       "4             False  False  False  False  False  False  False  False  False  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7a55fb7d-1086-4a6e-b080-a185daaefabc",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df.to_parquet(\"outputs/d2-events-230929.parquet\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22b53823",
   "metadata": {},
   "source": [
    "## Hotspots in time window of 2D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c695eddb",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_parquet(\"outputs/d2-events-230929.parquet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fafdf9dc",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df.set_index('time', inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "94a22fef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>GEOID</th>\n",
       "      <th>wfday</th>\n",
       "      <th>heatday</th>\n",
       "      <th>smoke_pm_non_zero</th>\n",
       "      <th>smoke_pm_gt_five</th>\n",
       "      <th>hw</th>\n",
       "      <th>_hws</th>\n",
       "      <th>hs</th>\n",
       "      <th>hws</th>\n",
       "      <th>ws</th>\n",
       "      <th>hs5</th>\n",
       "      <th>hws5</th>\n",
       "      <th>ws5</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2006-01-01</th>\n",
       "      <td>04001942600</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-01-01</th>\n",
       "      <td>04001942700</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-01-01</th>\n",
       "      <td>04001944000</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-01-01</th>\n",
       "      <td>04001944100</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-01-01</th>\n",
       "      <td>04001944201</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  GEOID  wfday  heatday  smoke_pm_non_zero  smoke_pm_gt_five  \\\n",
       "time                                                                           \n",
       "2006-01-01  04001942600  False    False              False             False   \n",
       "2006-01-01  04001942700  False    False              False             False   \n",
       "2006-01-01  04001944000  False    False              False             False   \n",
       "2006-01-01  04001944100  False    False              False             False   \n",
       "2006-01-01  04001944201  False    False              False             False   \n",
       "\n",
       "               hw   _hws     hs    hws     ws    hs5   hws5    ws5  \n",
       "time                                                                \n",
       "2006-01-01  False  False  False  False  False  False  False  False  \n",
       "2006-01-01  False  False  False  False  False  False  False  False  \n",
       "2006-01-01  False  False  False  False  False  False  False  False  \n",
       "2006-01-01  False  False  False  False  False  False  False  False  \n",
       "2006-01-01  False  False  False  False  False  False  False  False  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e80d7e17",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_22878/830991533.py:11: FutureWarning: Not prepending group keys to the result index of transform-like apply. In the future, the group keys will be included in the index, regardless of whether the applied function returns a like-indexed object.\n",
      "To preserve the previous behavior, use\n",
      "\n",
      "\t>>> .groupby(..., group_keys=False)\n",
      "\n",
      "To adopt the future behavior and silence this warning, use \n",
      "\n",
      "\t>>> .groupby(..., group_keys=True)\n",
      "  df = df.groupby(by=['GEOID']).apply(get_time_hotspot)\n"
     ]
    }
   ],
   "source": [
    "def get_time_hotspot(x):\n",
    "    x['heatday_2d'] = x['heatday'].rolling('2D', min_periods=1).max()\n",
    "    x['smoke_pm_non_zero_2d'] = x['smoke_pm_non_zero'].rolling('2D', min_periods=1).max()\n",
    "    x['wfday_2d'] = x['wfday'].rolling('2D', min_periods=1).max()\n",
    "    return x\n",
    "\n",
    "df['heatday_2d'] = False  # Initialize the 'new' column as False\n",
    "df['smoke_pm_non_zero_2d'] = False  # Initialize the 'new' column as False\n",
    "df['wfday_2d'] = False  # Initialize the 'new' column as False\n",
    "\n",
    "df = df.groupby(by=['GEOID']).apply(get_time_hotspot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8b4c66c0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df['heatday_2d'] = df['heatday_2d'].astype(int)\n",
    "df['smoke_pm_non_zero_2d'] = df['smoke_pm_non_zero_2d'].astype(int)\n",
    "df['wfday_2d'] = df['wfday_2d'].astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bb0cb98a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df['_hws_2d'] = df[['heatday_2d','wfday_2d','smoke_pm_non_zero_2d']].any(axis=1)\n",
    "df['hs_2d'] = df['heatday_2d'] & df['smoke_pm_non_zero_2d']\n",
    "df['hws_2d'] = df['heatday_2d'] & df['wfday_2d'] & df['smoke_pm_non_zero_2d']\n",
    "df['ws_2d'] = df['wfday_2d'] & df['smoke_pm_non_zero_2d']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "561703dd",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df.to_parquet(\"outputs/d2-events-2d-230929.parquet\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4c83fdac",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_parquet(\"outputs/d2-events-2d-230929.parquet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c8e1e250-6a30-4baf-90b0-dda387f609f7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_14223/1323442583.py:7: FutureWarning: Not prepending group keys to the result index of transform-like apply. In the future, the group keys will be included in the index, regardless of whether the applied function returns a like-indexed object.\n",
      "To preserve the previous behavior, use\n",
      "\n",
      "\t>>> .groupby(..., group_keys=False)\n",
      "\n",
      "To adopt the future behavior and silence this warning, use \n",
      "\n",
      "\t>>> .groupby(..., group_keys=True)\n",
      "  df = df.groupby(by=['GEOID']).apply(get_time_hotspot)\n"
     ]
    }
   ],
   "source": [
    "def get_time_hotspot(x):\n",
    "    x['smoke_pm_gt_five_2d'] = x['smoke_pm_gt_five'].rolling('2D', min_periods=1).max()\n",
    "    return x\n",
    "\n",
    "df['smoke_pm_gt_five_2d'] = False  \n",
    "\n",
    "df = df.groupby(by=['GEOID']).apply(get_time_hotspot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e7f95910-e3b1-4742-8e42-93a149e670b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['hw_2d'] = df['heatday_2d'] & df['wfday_2d']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2dadf94f-1018-4939-80aa-c67b20746240",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df['smoke_pm_gt_five_2d'] = df['smoke_pm_gt_five_2d'].astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "302b2909-99be-4f93-9a9c-7c8892a181ab",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df['hs_2d_5'] = df['heatday_2d'] & df['smoke_pm_gt_five_2d']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c0f1dfc7-ab77-444f-90f2-8bca769bab08",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df['ws_2d_5'] = df['wfday_2d'] & df['smoke_pm_gt_five_2d']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6d027ca7-d592-4f0e-928a-7b99f14e718f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df['hws_2d_5'] = df['heatday_2d'] & df['wfday_2d'] & df['smoke_pm_gt_five_2d']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4bdde4c6-ab70-4809-9eef-d358cf6b63dc",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df.to_parquet(\"outputs/d2-events-2d5-230929.parquet\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e7269ce-8c26-4c0d-905d-7c0cf3801c45",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
